package com.example.book.api;

import com.example.book.base.ApiBaseController;
import com.example.book.base.Result;
import com.example.book.common.annotation.LoginIgnore;
import com.example.book.in.entity.BookCategory;
import com.example.book.in.entity.BookInfo;
import com.example.book.in.entity.BookPopularity;
import com.example.book.in.service.BookCategoryService;
import com.example.book.in.service.BookInfoService;
import com.example.book.in.service.BookPopularityService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/book")
public class ApiBookPopularityController extends ApiBaseController {

    private BookPopularityService popularityService;
    private BookInfoService bookInfoService;
    private BookCategoryService categoryService;

    private ApiBookPopularityController(BookPopularityService popularityService, BookInfoService bookInfoService, BookCategoryService categoryService) {
        this.popularityService = popularityService;
        this.bookInfoService = bookInfoService;
        this.categoryService = categoryService;
    }

    /**
     * 查询首页的人气值大的书籍
     *
     * @return
     */
    @GetMapping("/popularity/front")
    @LoginIgnore
    public Result findFrontList() {
        List<BookPopularity> popularities = popularityService.findFrontList(4);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = null;
        BookInfo bookInfo = null;
        for (BookPopularity bp : popularities) {

            bookInfo = new BookInfo();
            bookInfo.setId(bp.getBookInfoId());
            BookInfo bi = bookInfoService.get(bookInfo);

            if (bi==null){
                continue;
            }

            map = new HashMap<>();
            map.put("id", bi.getId());
            map.put("picture", picture_url + bi.getBookCover());
            if (bi.getBookName().length() > 11) {
                String idt = bi.getBookName().substring(0, 11) + "...";
                map.put("name", idt);
            } else {
                map.put("name", bi.getBookName());
            }
            list.add(map);
        }

        return Result.success(list);
    }

    /**
     * 查询人气值分页书籍
     *
     * @param popularity
     * @return
     */
    @GetMapping("/popularity/info")
    @LoginIgnore
    public Result findPopularityList(BookPopularity popularity) {

        PageInfo<BookPopularity> pageInfo = popularityService.findListByLimit(popularity);

        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> map = null;
        BookInfo bookInfo = new BookInfo();
        BookCategory category = new BookCategory();
        for (BookPopularity bp : pageInfo.getList()) {

            bookInfo.setId(bp.getBookInfoId());
            bookInfo.setStatus("1");
            BookInfo bi = bookInfoService.get(bookInfo);
            if (bi != null) {
                map = new HashMap<>();
                map.put("id", bi.getId());
                map.put("picture", picture_url + bi.getBookCover());

                category.setId(bi.getCategoryId());
                BookCategory bc = categoryService.get(category);
                if (bc != null) {
                    map.put("category", bc.getName());
                } else {
                    map.put("category", "未分类");
                }

                if (bi.getBookName().length() > 11) {
                    String idt = bi.getBookName().substring(0, 11) + "...";
                    map.put("name", idt);
                } else {
                    map.put("name", bi.getBookName());
                }

                map.put("author", bi.getBookAuthor().replace("作者：", ""));

                if (bi.getBookIntroduction().length() > 46) {
                    String idt = bi.getBookIntroduction().substring(0, 46) + "...";
                    map.put("introduction", idt);
                } else {
                    if (StringUtils.isBlank(bi.getBookIntroduction())) {
                        map.put("introduction", "^^...");
                    } else {
                        map.put("introduction", bi.getBookIntroduction());
                    }
                }

                // 人气
                BookPopularity bookPopularity = new BookPopularity();
                bookPopularity.setBookInfoId(bi.getId());
                List<BookPopularity> popularities = popularityService.findList(bookPopularity);
                map.put("popularity", popularities.size());
                list.add(map);
            }

        }

        Map<String, Object> map1 = new HashMap<>();
        map1.put("isHasNextPage", pageInfo.isHasNextPage());
        map1.put("data", list);
        return Result.success("success", map1);

    }

    /**
     * 每次浏览到详情页的记录 不记名
     *
     * @param popularity
     * @return
     */
    @PostMapping("/popularity")
    @LoginIgnore
    public Result addPopularity(BookPopularity popularity) {
        popularityService.save(popularity);
        return Result.success();
    }

}
